<!DOCTYPE html>
<html lang=zh>
<head><meta name="generator" content="Hexo 3.9.0">
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="description" content="在开发移动端的时候，经常会有这样的需求—有一个弹窗里面包含了一个登陆框，登录框需要对用户输入的值做验证，在值不正确的情况下使用 alert 提示用户输入有误。 在 Android 机上使用 fixed 来定位登录框是没有问题的，但是在 iphone 上 bug 就存在了，先来看看我录制的两个视频: 您的浏览器不支持视频播放  注意看弹出 alert 的一瞬间透明背景的高度，是不是有些问题？再看另一">
<meta property="og:type" content="article">
<meta property="og:title" content="修复iphone下有输入框弹窗fixed失效的bug">
<meta property="og:url" content="https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/index.html">
<meta property="og:site_name" content="月光倾城的代码小屋">
<meta property="og:description" content="在开发移动端的时候，经常会有这样的需求—有一个弹窗里面包含了一个登陆框，登录框需要对用户输入的值做验证，在值不正确的情况下使用 alert 提示用户输入有误。 在 Android 机上使用 fixed 来定位登录框是没有问题的，但是在 iphone 上 bug 就存在了，先来看看我录制的两个视频: 您的浏览器不支持视频播放  注意看弹出 alert 的一瞬间透明背景的高度，是不是有些问题？再看另一">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://file.lantingshucheng.com/blog/post20170912_01.png/default">
<meta property="og:image" content="https://file.lantingshucheng.com/blog/post20170912_02.png/default">
<meta property="og:image" content="https://file.lantingshucheng.com/blog/post20170912_03.png/default">
<meta property="og:image" content="https://file.lantingshucheng.com/blog/post20170912_05.png/default">
<meta property="og:updated_time" content="2020-01-15T06:30:09.116Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="修复iphone下有输入框弹窗fixed失效的bug">
<meta name="twitter:description" content="在开发移动端的时候，经常会有这样的需求—有一个弹窗里面包含了一个登陆框，登录框需要对用户输入的值做验证，在值不正确的情况下使用 alert 提示用户输入有误。 在 Android 机上使用 fixed 来定位登录框是没有问题的，但是在 iphone 上 bug 就存在了，先来看看我录制的两个视频: 您的浏览器不支持视频播放  注意看弹出 alert 的一瞬间透明背景的高度，是不是有些问题？再看另一">
<meta name="twitter:image" content="https://file.lantingshucheng.com/blog/post20170912_01.png/default">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
          
        
    
    <!-- title -->
    <title>修复iphone下有输入框弹窗fixed失效的bug</title>
    <!-- styles -->
    <link rel="stylesheet" href="/css/style.css">
    <!-- persian styles -->
    
      <link rel="stylesheet" href="/css/rtl.css">
    
    <!-- rss -->
    
    
</head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/2017/10/17/sublime_sftp_txy/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/2017/09/11/vscode_debug_python/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">上一篇</span>
      <span id="i-next" class="info" style="display:none;">下一篇</span>
      <span id="i-top" class="info" style="display:none;">返回顶部</span>
      <span id="i-share" class="info" style="display:none;">分享文章</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/&text=修复iphone下有输入框弹窗fixed失效的bug"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/&title=修复iphone下有输入框弹窗fixed失效的bug"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/&is_video=false&description=修复iphone下有输入框弹窗fixed失效的bug"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=修复iphone下有输入框弹窗fixed失效的bug&body=Check out this article: https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/&title=修复iphone下有输入框弹窗fixed失效的bug"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/&title=修复iphone下有输入框弹窗fixed失效的bug"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/&title=修复iphone下有输入框弹窗fixed失效的bug"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/&title=修复iphone下有输入框弹窗fixed失效的bug"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/&name=修复iphone下有输入框弹窗fixed失效的bug&description="><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#bug-产生的原因"><span class="toc-number">1.</span> <span class="toc-text">bug 产生的原因</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        修复iphone下有输入框弹窗fixed失效的bug
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">月光倾城的代码小屋</span>
      </span>
      
    <div class="postdate">
        <time datetime="2017-09-12T17:25:10.000Z" itemprop="datePublished">2017-09-12</time>
    </div>


      

      

    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <p>在开发移动端的时候，经常会有这样的需求—有一个弹窗里面包含了一个登陆框，登录框需要对用户输入的值做验证，在值不正确的情况下使用 alert 提示用户输入有误。</p>
<p>在 Android 机上使用 fixed 来定位登录框是没有问题的，但是在 iphone 上 bug 就存在了，先来看看我录制的两个视频:</p>
<video style="width: 600px" src="https://file.lantingshucheng.com/blog/post20170912_v02.mp4" controls="controls" height="400px"><br>您的浏览器不支持视频播放<br></video>

<p>注意看弹出 alert 的一瞬间透明背景的高度，是不是有些问题？再看另一个：</p>
<p><video style="height: 400px" src="https://file.lantingshucheng.com/blog/post20170912_v03.mp4" controls="controls" height="400px"><br>您的浏览器不支持视频播放<br></video><br>以上问题还没看明白的话，我这儿还有个两个的 demo，掏出手机扫描下看看吧。左边是错误的 demo，右边是正确的 demo。点击”1000 元起始投资“后再点击”我已开户“才能看到登陆弹窗哦~</p>
<div style="overflow: hidden; text-align: center"><div style="display: inline-block; width: 200px;"><img src="https://file.lantingshucheng.com/blog/post20170912_01.png/default"><p style="text-align: center">错误的demo</p></div><div style="display: inline-block; width: 200px; margin-left: 40px"><img src="https://file.lantingshucheng.com/blog/post20170912_02.png/default"><p style="text-align: center">正确的demo</p></div></div>

<h4 id="bug-产生的原因"><a href="#bug-产生的原因" class="headerlink" title="bug 产生的原因"></a><strong>bug 产生的原因</strong></h4><p>iphone 在键盘弹出的时候，页面的高度为屏幕高度减去键盘的高度，当 input 元素失去焦点，键盘的收起的 0.5s 内弹出了 alert 框，js 被停止执行，并且弹窗的 fixed 属性失效了。由于透明的黑色背景也是采用的 fixed 定位，所以在 fixed 失效的时透明的黑色背景的高度不会随着 body 的高度变大(键盘往下收缩，腾出来的空间显示页面)而变大。</p>
<p><div style="text-align: center"><img style="display: inline-block; width: 300px" src="https://file.lantingshucheng.com/blog/post20170912_03.png/default"> <img style="display: inline-block; width: 300px; margin-left: 30px;" src="https://file.lantingshucheng.com/blog/post20170912_05.png/default"></div><br>根据以上分析，导致这种现象的有两个原因:<br><strong>第一</strong>：使用了fixed定位<br><strong>第二</strong>： alert弹出的时机太早了，应该等到键盘完全收起之后再弹出，这样就不会阻碍js的执行了。</p>
<p>所以改良之后的弹窗方案如下：</p>
<p><strong>html</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span>页面主体内容<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"alert-container"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"content"</span> <span class="attr">style</span>=<span class="string">"margin-top: -156px;"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"title"</span>&gt;</span></span><br><span class="line">        请先登录</span><br><span class="line">        <span class="tag">&lt;<span class="name">i</span> <span class="attr">class</span>=<span class="string">"icon-close"</span>&gt;</span><span class="tag">&lt;/<span class="name">i</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"login"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">input</span> <span class="attr">id</span>=<span class="string">"phone"</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">placeholder</span>=<span class="string">"请输入手机号"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"yzm-container"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">input</span> <span class="attr">id</span>=<span class="string">"img-yzm"</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">placeholder</span>=<span class="string">"请输入图形验证码"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">img</span> <span class="attr">id</span>=<span class="string">"imagecodeSrc"</span> <span class="attr">src</span>=<span class="string">"//khtest.10jqka.com.cn/kh/apiprize/index.php?action=imgcode"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"yzm-container"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">input</span> <span class="attr">id</span>=<span class="string">"phone-yzm"</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">placeholder</span>=<span class="string">"请输入手机验证码"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">"getPhoneYzm"</span>&gt;</span>获取验证码<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">"doLogin"</span> <span class="attr">class</span>=<span class="string">"btn"</span>&gt;</span>立即登录<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"modal-bg"</span>&gt;</span> <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><strong>css</strong></p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 禁止body滚动，设置container滚动，因为接下来弹框需要使用absolute定位，弹框是body的子元素，body高度不定位为视窗高度的话，使用绝对定位无法将弹窗垂直居中。*/</span></span><br><span class="line"><span class="selector-tag">html</span>,</span><br><span class="line"><span class="selector-tag">body</span> &#123;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">overflow</span>: hidden;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/* 这里container的height要是100%，不然container无法滚动了*/</span></span><br><span class="line"><span class="selector-class">.container</span> &#123;</span><br><span class="line">  <span class="attribute">overflow-x</span>: hidden;</span><br><span class="line">  <span class="attribute">overflow-y</span>: auto;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">100%</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/* 黑色透明背景，使用觉得定位，弹窗的margin-top值再创建弹窗的时候使用js动态计算，确保弹窗垂直居中*/</span></span><br><span class="line"><span class="selector-class">.modal-bg</span> &#123;</span><br><span class="line">  <span class="attribute">top</span>: <span class="number">0</span>;</span><br><span class="line">  <span class="attribute">left</span>: <span class="number">0</span>;</span><br><span class="line">  <span class="attribute">bottom</span>: <span class="number">0</span>;</span><br><span class="line">  <span class="attribute">right</span>: <span class="number">0</span>;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">z-index</span>: <span class="number">900</span>;</span><br><span class="line">  <span class="attribute">overflow</span>: hidden;</span><br><span class="line">  <span class="attribute">position</span>: absolute;</span><br><span class="line">  <span class="attribute">background</span>: <span class="built_in">rgba</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0.7</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/* 弹出框，也使用绝对定位*/</span></span><br><span class="line"><span class="selector-class">.alert-container</span> &#123;</span><br><span class="line">  <span class="attribute">top</span>: <span class="number">0</span>;</span><br><span class="line">  <span class="attribute">left</span>: <span class="number">0</span>;</span><br><span class="line">  <span class="attribute">bottom</span>: <span class="number">0</span>;</span><br><span class="line">  <span class="attribute">right</span>: <span class="number">0</span>;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">overflow</span>: hidden;</span><br><span class="line">  <span class="attribute">z-index</span>: <span class="number">1000</span>;</span><br><span class="line">  <span class="attribute">text-align</span>: center;</span><br><span class="line">  <span class="attribute">position</span>: fixed;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.alert-container</span> <span class="selector-class">.content</span> &#123;</span><br><span class="line">  <span class="attribute">background</span>: <span class="number">#fff</span>;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#2c2c2c</span>;</span><br><span class="line">  <span class="attribute">background-size</span>: <span class="number">100%</span> <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">position</span>: relative;</span><br><span class="line">  <span class="attribute">left</span>: <span class="number">8%</span>;</span><br><span class="line">  <span class="attribute">top</span>: <span class="number">50%</span>;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">84%</span>;</span><br><span class="line">  <span class="attribute">z-index</span>: <span class="number">2000</span>;</span><br><span class="line">  <span class="attribute">border-radius</span>: <span class="number">0.1rem</span>;</span><br><span class="line">  <span class="attribute">padding</span>: <span class="number">0</span> <span class="number">0.36rem</span>;</span><br><span class="line">  <span class="attribute">box-sizing</span>: border-box;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.alert-container</span> <span class="selector-class">.content</span> <span class="selector-class">.title</span> &#123;</span><br><span class="line">  <span class="attribute">padding</span>: <span class="number">0.24rem</span> <span class="number">0</span> <span class="number">0.18rem</span> <span class="number">0</span>;</span><br><span class="line">  <span class="attribute">font-size</span>: <span class="number">0.32rem</span>;</span><br><span class="line">  <span class="attribute">border-bottom</span>: <span class="number">1px</span> solid <span class="number">#dcdcdc</span>;</span><br><span class="line">  <span class="attribute">position</span>: relative;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.alert-container</span> <span class="selector-class">.content</span> <span class="selector-class">.title</span> <span class="selector-class">.icon-close</span> &#123;</span><br><span class="line">  <span class="attribute">display</span>: inline-block;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">0.35rem</span>;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">0.35rem</span>;</span><br><span class="line">  <span class="attribute">position</span>: absolute;</span><br><span class="line">  <span class="attribute">top</span>: <span class="number">50%</span>;</span><br><span class="line">  <span class="attribute">margin-top</span>: -<span class="number">0.16rem</span>;</span><br><span class="line">  <span class="attribute">right</span>: <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.alert-container</span> <span class="selector-class">.content</span> <span class="selector-class">.login</span> &#123;</span><br><span class="line">  <span class="attribute">padding-top</span>: <span class="number">0.3rem</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.alert-container</span> <span class="selector-class">.content</span> <span class="selector-class">.login</span> <span class="selector-tag">input</span> &#123;</span><br><span class="line">  <span class="attribute">outline</span>: none;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">0.76rem</span>;</span><br><span class="line">  <span class="attribute">font-size</span>: <span class="number">0.26rem</span>;</span><br><span class="line">  <span class="attribute">vertical-align</span>: middle;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.alert-container</span> <span class="selector-class">.content</span> <span class="selector-class">.login</span> <span class="selector-id">#phone</span> &#123;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">border</span>: none;</span><br><span class="line">  <span class="attribute">border-bottom</span>: <span class="number">1px</span> solid <span class="number">#dcdcdc</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.alert-container</span> <span class="selector-class">.content</span> <span class="selector-class">.login</span> <span class="selector-class">.yzm-container</span> &#123;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">0.76rem</span>;</span><br><span class="line">  <span class="attribute">white-space</span>: nowrap;</span><br><span class="line">  <span class="attribute">border-bottom</span>: <span class="number">1px</span> solid <span class="number">#dcdcdc</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.alert-container</span> <span class="selector-class">.content</span> <span class="selector-class">.login</span> <span class="selector-class">.yzm-container</span> &gt; <span class="selector-tag">input</span> &#123;</span><br><span class="line">  <span class="attribute">float</span>: left;</span><br><span class="line">  <span class="attribute">border</span>: none;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">padding-right</span>: <span class="number">1.6rem</span>;</span><br><span class="line">  <span class="attribute">box-sizing</span>: border-box;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.alert-container</span> <span class="selector-class">.content</span> <span class="selector-class">.login</span> <span class="selector-class">.yzm-container</span> &gt; <span class="selector-tag">img</span> &#123;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">0.5rem</span>;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">1.4rem</span>;</span><br><span class="line">  <span class="attribute">margin-top</span>: <span class="number">0.13rem</span>;</span><br><span class="line">  <span class="attribute">vertical-align</span>: middle;</span><br><span class="line">  <span class="attribute">float</span>: left;</span><br><span class="line">  <span class="attribute">margin-left</span>: -<span class="number">1.4rem</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.alert-container</span> <span class="selector-class">.content</span> <span class="selector-class">.login</span> <span class="selector-id">#getPhoneYzm</span> &#123;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">1.4rem</span>;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">0.76rem</span>;</span><br><span class="line">  <span class="attribute">float</span>: right;</span><br><span class="line">  <span class="attribute">background</span>: none;</span><br><span class="line">  <span class="attribute">border</span>: none;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#5589ff</span>;</span><br><span class="line">  <span class="attribute">margin-left</span>: -<span class="number">1.4rem</span>;</span><br><span class="line">  <span class="attribute">font-size</span>: <span class="number">0.26rem</span>;</span><br><span class="line">  <span class="attribute">outline</span>: none;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>javascript</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 创建弹窗</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createAlertDiv</span>(<span class="params">title, content, closeStat</span>) </span>&#123;</span><br><span class="line">  <span class="comment">//对于已经存在的alert先去掉</span></span><br><span class="line">  $(<span class="string">'.alert-container'</span>).remove();</span><br><span class="line">  $(<span class="string">'.modal-bg'</span>).remove();</span><br><span class="line">  <span class="comment">//动态生成dom</span></span><br><span class="line">  <span class="keyword">try</span> &#123;</span><br><span class="line">    <span class="keyword">var</span> alertHtml = <span class="string">'&lt;div class="alert-container"&gt;&lt;div class="content"&gt;&lt;div class="title"&gt;'</span> + title + <span class="string">'&lt;i class="icon-close"&gt;&lt;/i&gt;     &lt;/div&gt;'</span> + content + <span class="string">'&lt;/div&gt;&lt;div class="modal-bg"&gt;&lt;/div&gt;&lt;/div&gt;'</span></span><br><span class="line">    $(<span class="string">'body'</span>).append(alertHtml)</span><br><span class="line">    <span class="comment">// 根据弹窗的实际高度动态计算弹窗的margin-top值，这里不使用transform是为了低端机兼容</span></span><br><span class="line">    $(<span class="string">'.alert-container .content'</span>).css(<span class="string">'margin-top'</span>, -($(<span class="string">'.alert-container .content'</span>).height() / <span class="number">2</span>) + <span class="string">'px'</span>)</span><br><span class="line">    $(<span class="string">'.modal-bg, .icon-close'</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="comment">// 如果是登录弹窗，关闭弹窗应当还原登陆弹窗数据</span></span><br><span class="line">      <span class="keyword">if</span> ($(<span class="string">'.alert-container .login'</span>).length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        skeyArr = []</span><br><span class="line">        telphone = <span class="string">''</span></span><br><span class="line">        isPhoneCorrect = <span class="literal">false</span></span><br><span class="line">        imageCode = <span class="string">''</span></span><br><span class="line">        isImageCodeCorrect = <span class="literal">false</span></span><br><span class="line">        isGetYzmCanBeClick = <span class="literal">true</span></span><br><span class="line">        phoneYzm = <span class="string">''</span></span><br><span class="line">        isPhoneYzmCorrect = <span class="literal">false</span></span><br><span class="line">        clearInterval(yzmTimer);</span><br><span class="line">        yzmTimer = <span class="literal">null</span></span><br><span class="line">      &#125;</span><br><span class="line">      $(<span class="string">'.alert-container'</span>).remove();</span><br><span class="line">      $(<span class="string">'.modal-bg'</span>).remove();</span><br><span class="line">      <span class="comment">// 关闭弹窗统计</span></span><br><span class="line">      hxmClickStat(pageId + <span class="string">'.'</span> + closeStat)</span><br><span class="line">   &#125;)</span><br><span class="line">   <span class="comment">//阻止屏幕滑动</span></span><br><span class="line">   $(<span class="string">'.alert-container'</span>).bind(<span class="string">'touchmove'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">e</span>) </span>&#123;</span><br><span class="line">     e.preventDefault();</span><br><span class="line">   &#125;);</span><br><span class="line">  &#125; <span class="keyword">catch</span> (err) &#123;</span><br><span class="line">   <span class="built_in">console</span>.log(err)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 弹出错误提示的时候需要延时500ms</span></span><br><span class="line">....</span><br><span class="line">$.ajax(&#123;</span><br><span class="line">  url: xxx,</span><br><span class="line">  success: <span class="function"><span class="keyword">function</span>(<span class="params">res</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(res.data.code === <span class="string">'0'</span>)&#123;</span><br><span class="line">        <span class="comment">// 正确的处理逻辑</span></span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        iphoneDelayDone(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;alert(res.data.msg)&#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line">....</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">iphoneDelayDone</span>(<span class="params">callback</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (platform === <span class="string">'iphone'</span>) &#123;</span><br><span class="line">    setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="keyword">if</span> (<span class="keyword">typeof</span> callback === <span class="string">'function'</span>) &#123;</span><br><span class="line">        callback()</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;, <span class="number">500</span>)</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> callback === <span class="string">'function'</span>) &#123;</span><br><span class="line">      callback()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>最后贴一个正确效果的视频。</p>
<video style="height: 400px" src="https://file.lantingshucheng.com/blog/post20170912_v01.mp4" controls="controls" height="400px">
</video>
  </div>
  <div class="statement">
    <hr>  
    1. 如果本文帮到了您，不妨点一下右上角的”<i class="fas fa-share-alt"></i>“按钮。<br>
    2. 除非注明，本博文章均为原创，转载请以链接形式标明本文地址。<br>
    3. 如果你有任何疑问，欢迎发送邮件到我的邮箱<a href="mailto:andyliwr@outlook.com">andyliwr@outlook.com<br>
  </div>
</article>


    <div class="blog-post-comments">
        <div id="gitalk_thread">
            <noscript>加载评论需要在浏览器启用 JavaScript 脚本支持。</noscript>
        </div>
    </div>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#bug-产生的原因"><span class="toc-number">1.</span> <span class="toc-text">bug 产生的原因</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/&text=修复iphone下有输入框弹窗fixed失效的bug"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/&title=修复iphone下有输入框弹窗fixed失效的bug"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/&is_video=false&description=修复iphone下有输入框弹窗fixed失效的bug"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=修复iphone下有输入框弹窗fixed失效的bug&body=Check out this article: https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/&title=修复iphone下有输入框弹窗fixed失效的bug"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/&title=修复iphone下有输入框弹窗fixed失效的bug"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/&title=修复iphone下有输入框弹窗fixed失效的bug"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/&title=修复iphone下有输入框弹窗fixed失效的bug"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://andyliwr.github.io/2017/09/12/iphone_dialog_fixed/&name=修复iphone下有输入框弹窗fixed失效的bug&description="><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> 菜单</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> 目录</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> 分享</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> 返回顶部</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2020 Dikang Li
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
</body>
</html>
<!-- styles -->
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">
<link rel="stylesheet" href="/lib/fancybox/jquery.fancybox.css">

<link rel="stylesheet" href="https://file.lantingshucheng.com/blog/v3/static/styles/gitalk.css">


<!-- jquery -->
<script src="/lib/jquery/jquery.min.js"></script>
<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>
<script src="/lib/fancybox/jquery.fancybox.pack.js"></script>

<script type="text/javascript" src="https://file.lantingshucheng.com/blog/v3/static/scripts/md5.js"></script>
<script type="text/javascript" src="https://file.lantingshucheng.com/blog/v3/static/scripts/gitalk.min.js"></script>

<script src="/js/main.js"></script>
<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

    <script type="text/javascript">
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?7851ca7a86be957be994acb77639a5be";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();
    </script>

<!-- Disqus Comments -->


  <script type="text/javascript">
    window.onload = function(){
      if (window.location.href.indexOf('andyliwr.github.io') > -1) {
        var gitalk = new Gitalk({
          clientID: 'ad6f70df0c70d2b715f9',
          clientSecret: '40384bee244c1e17dd7c37733d21e9ef669e969c',
          repo: 'Andyliwr.github.io',
          owner: 'Andyliwr',
          admin: ['Andyliwr'],
          id: md5(location.pathname),
          distractionFreeMode: 'true'
        })
        gitalk.render('gitalk_thread')
      }
    }
  </script>


